
let superagent = require('superagent'); // 发送请求
let cheerio = require('cheerio');// cheerio : 解析网页
let express = require('express');
let db = require('./db/db');
let app = express();

let pathColl = [
  // '/category/ECMAScript.md',
  // '/category/css.md',
  // '/category/html.md',
  // '/category/js.md',
  '/category/nodejs.md',
  // '/category/skill.md',
  // '/category/week.md',
  // '/lib/React.md',
  // '/lib/Vue.md',
  // '/lib/jQuery.md',
];

/**
 * @param res
 * @returns {Array}
 */


function getHostRes(res) {
  // let $ = cheerio.load(res.text);
  let $ = cheerio.load(res);
  // $ 是一个函数

  console.log($);
  let localArr = [];

  $('.markdown-body ul li a').each((ind, ele) => {

    let obj = {
      title: $(ele).text(),// a标签的文字
      href: $(ele).attr('href')
    };
    localArr.push(obj);
  });
  return localArr;
}

// promise all
function request(url) {
  return new Promise((resolve, reject) => {
    superagent.get(url).end((err, res) => {
      if (err) {
        resolve('抓取失败');
      } else {
        let hostRes = getHostRes(res.text);
        // 入库
        resolve(hostRes);
      }
    });
    // let hostRes = getHostRes(content);
    // // 入库
    // resolve(hostRes);
  })
}

// app.use(function (req, res) {
let promiseArr = [];
pathColl.forEach(item => {
  console.log(`https://github.com/haizlin/fe-interview/blob/master${item}`);
  promiseArr.push(request(`https://github.com/haizlin/fe-interview/blob/master${item}`));
});
Promise.all(promiseArr).then(res => {

  for (let i = 0; i < res.length; i++) {
    res[i].forEach(item => {
      let arrs = pathColl[i].split('/');
      let title = `${item.title}`;
      db.query(`insert into inter (type,title,href) VALUES ("${arrs[arrs.length - 1].split('.')[0]}","${title.split('\"').join('')}","${item.href}")`, (err, res) => {
        if (err) {
          console.log(err);
        }
      });
    })
  }
});

// 入库

// });

// app.listen(3000);


